package com.manlost.biz.common.ibatis;

import java.sql.SQLException;
import java.util.Collection;
import java.util.List;

import org.springframework.dao.DataAccessException;
import org.springframework.orm.ibatis.SqlMapClientCallback;
import org.springframework.orm.ibatis.SqlMapClientTemplate;

import com.ibatis.sqlmap.client.SqlMapExecutor;
import com.manlost.biz.common.query.PageQuery;

/**
 * @author wqq
 */
@SuppressWarnings("unchecked")
public class ManlostSqlMapClientTemplate extends SqlMapClientTemplate {

    public <T> PageQueryList<T> queryForPage(String countStatementName, String queryStatementName, PageQuery query) {
        PageQueryList<T> queryList = new PageQueryList<T>(query);
        Object obj = queryForObject(countStatementName, query);
        long totalCount = 0;
        if (obj instanceof Long) {
            totalCount = (Long) obj;
        } else if (obj instanceof Integer) {
            totalCount = (Integer) obj;
        } else {
            throw new IllegalArgumentException();
        }
        queryList.setTotalCount(totalCount);

        if (totalCount > 0) {
            List<T> items = queryForList(queryStatementName, query);

            if (items != null) {
                queryList.addAll(items);
            }
        }

        return queryList;
    }

    /**
     * 批量插入
     * 
     * @param statementName
     * @param parameterObjects
     * @return
     * @throws DataAccessException
     */
    public Long batchInsert(final String statementName, final Collection<? extends Object> parameterObjects)
                                                                                                            throws DataAccessException {
        return batchAction(statementName, parameterObjects, new BatchAction() {

            public void doAction(final SqlMapExecutor executor, final String statementName, final Object parameterObject)
                                                                                                                         throws SQLException {
                executor.insert(statementName, parameterObject);
            }
        });
    }

    @SuppressWarnings("rawtypes")
    private Long batchAction(final String statementName, final Collection<? extends Object> parameterObjects,
                             final BatchAction batchAction) throws DataAccessException {
        Object ret = execute(new SqlMapClientCallback() {

            public Object doInSqlMapClient(SqlMapExecutor executor) throws SQLException {
                if (parameterObjects == null || parameterObjects.isEmpty()) {
                    return new Long(0);
                } else {
                    executor.startBatch();
                    for (Object parameterObject : parameterObjects) {
                        batchAction.doAction(executor, statementName, parameterObject);
                    }
                    return new Long(executor.executeBatch());
                }
            }
        });

        if (ret instanceof Long) {
            return (Long) ret;
        } else {
            return new Long(0);
        }
    }
}

/**
 * 基本操作接口
 * 
 * @author wqq
 */
interface BatchAction {

    public void doAction(final SqlMapExecutor executor, final String statementName, final Object parameterObject)
                                                                                                                 throws SQLException;
}
